
/*
 * @Author: Evan-lian
 * @Date: 2023-12-25 17:10:12
 * @LastEditors: Evan-lian
 * @LastEditTime: 2023-12-28 17:32:26
 * @Description: 
 */
import { Dispatch} from "redux"; 
import { login as loginApi,getUserMenu} from "../../api/auth";
import {SET_TOKEN,SET_USER,SET_MENU} from '../Types'
import { UserType } from "../../types";
export function login(data:UserType,callback:Function){
    return (dispatch:Dispatch<any>)=>{
        loginApi(data)
        .then((res:any)=>{
            console.log(res)
            if(res.data && res.data.code===200){
                sessionStorage.setItem("token",res.data.token)
                sessionStorage.setItem("userInfo",JSON.stringify(res.data.user))
                dispatch({type:SET_TOKEN,payload:res.data.token})
                dispatch({type:SET_USER,payload:res.data.user})
                callback && callback()
                //登录成功后获取菜单
                dispatch(getMenu())
            }
        })
    }
}
export function getMenu(){
    return (dispatch:any)=>{
        getUserMenu().then(res=>{
            console.log(res.data.list)
            dispatch({type:SET_MENU,payload:res.data.list})
        })
    }
}

